#coding: utf-8

from __future__ import unicode_literals

from django.db import models

# Create your models here.

from django.db import models
from django.utils import timezone
from django.forms import Textarea
from lunardate import LunarDate
from datetime import date, timedelta
import traceback
import sys

reload(sys)
sys.setdefaultencoding('utf8')

'''
镇
'''
class Town(models.Model):
    name = models.CharField(u"乡镇名", max_length=20, unique=True)
    def __unicode__(self):
        return u"%s" % (self.name)

'''
村
'''
class Village(models.Model):
    town = models.ForeignKey(Town, on_delete=models.CASCADE)
    name = models.CharField(u"村名", max_length=20, unique=True)
    def __unicode__(self):
        return u"%s" % (self.name)

'''
死亡人员表
'''
class Deadman(models.Model):
    village = models.ForeignKey(Village, on_delete=models.CASCADE)
    deadman_name = models.CharField(u"死者姓名", max_length=10)
    kin_name = models.CharField(u"亲属姓名", max_length=10)
    phone = models.CharField(u"联系电话", max_length=16)
    dead_date = models.DateField(u"阳历死亡日期")
    memo = models.CharField(u"备注", max_length=80, blank=True)
    def GetSacrificeDay(self):
        day_map = {}
        try:
            # 输入日期为阳历
            d = self.dead_date
            # 转换为阴历
            ld = LunarDate.fromSolarDate(d.year, d.month, d.day)
            #死亡时间
            day_map['d0_d'] = d
            day_map['d0_ld'] = "%s年%s月%s日" % (ld.year, ld.month, ld.day)
            #烧头七时间
            _d = d + timedelta(1*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d1_7_d'] = _d
            day_map['d1_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧二七时间
            _d = d + timedelta(2*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d2_7_d'] = _d
            day_map['d2_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧三七时间
            _d = d + timedelta(3*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d3_7_d'] = _d
            day_map['d3_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧四七时间
            _d = d + timedelta(4*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d4_7_d'] = _d
            day_map['d4_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧五七时间
            _d = d + timedelta(5*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d5_7_d'] = _d
            day_map['d5_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧六七时间
            _d = d + timedelta(6*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d6_7_d'] = _d
            day_map['d6_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧七七时间
            _d = d + timedelta(7*7-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d7_7_d'] = _d
            day_map['d7_7_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧百日时间
            _d = d + timedelta(100-1)
            _ld = LunarDate.fromSolarDate(_d.year, _d.month, _d.day)
            day_map['d100_d'] = _d
            day_map['d100_ld'] = "%s年%s月%s日" % (_ld.year, _ld.month, _ld.day)
            #烧周年时间
            ld.year += 1
            day_map['y1_d'] = ld.toSolarDate()
            day_map['y1_ld'] = "%s年%s月%s日" % (ld.year, ld.month, ld.day)
            #烧二周年时间
            ld.year += 1
            day_map['y2_d'] = ld.toSolarDate()
            day_map['y2_ld'] = "%s年%s月%s日" % (ld.year, ld.month, ld.day)
            #烧三周年时间
            ld.year += 1
            day_map['y3_d'] = ld.toSolarDate()
            day_map['y3_ld'] = "%s年%s月%s日" % (ld.year, ld.month, ld.day)
        except Exception, e:
            print "error:", str(e)
            print "deadman:", self.__unicode__()
            traceback.print_exc()
        return day_map

    @classmethod
    def GetSacrificeExplain(self):
        explain_map = {}
        explain_map["d0_d"] = "死亡时间"
        explain_map["d1_7_d"] = "烧一七时间"
        explain_map["d2_7_d"] = "烧二七时间"
        explain_map["d3_7_d"] = "烧三七时间"
        explain_map["d4_7_d"] = "烧四七时间"
        explain_map["d5_7_d"] = "烧五七时间"
        explain_map["d6_7_d"] = "烧六七时间"
        explain_map["d7_7_d"] = "烧七七时间"
        explain_map["d100_d"] = "烧百日时间"
        explain_map["y1_d"] = "烧周年时间"
        explain_map["y2_d"] = "烧二周年时间"
        explain_map["y3_d"] = "烧三周年时间"
        return explain_map

    def __unicode__(self):
        return u"%s-%s@%s" % (self.deadman_name, self.village.name, self.dead_date)

'''
管理员
'''
class Manager(models.Model):
    name = models.CharField(u"姓名", max_length=10)
    phone = models.CharField(u"联系电话", max_length=16)
    email = models.EmailField(u"电子邮件", max_length=254)
    def __unicode__(self):
        return u"%s" % (self.name,)


if __name__ == '__main__':
    pass
